#include "common.h"
#include "alarm.h"
#include "part.h"
#include "upper.h"
#include "user.h"

timer* sample_timer = NULL;
void scan_func(void* param);

device_alarm_list alarm_list;
extern user_socket* user_array[MAX_LOGINS];

void alram_info_register(device_alarm_list* p_list,int* p_value,char* value_string,char* value_name,char* device){
    if(p_list->index < MAX_ALARM_COUNT){
        p_list->alarms[p_list->index].p_value = p_value;
        strcpy(p_list->alarms[p_list->index].value_string,value_string);
        strcpy(p_list->alarms[p_list->index].value_name,value_name);
        strcpy(p_list->alarms[p_list->index].device,device);
        p_list->index++;
    }
}

void alarm_sample_func(void* param){
    int i;
    for(i=0;i<MAX_LOGINS;i++){
        if(user_array[i]){
            user_array[i]->alarm_sample_flag = 1;
            break;
        }
    }
}

void init_alarm(){
    memset(&alarm_list,0,sizeof(device_alarm));
    part_alarm_register(&alarm_list);
    sample_timer = timer_start(alarm_sample_func,5*20,NULL,1);
}



void reset_alarm_info(){
    device_alarm* p_alarm;
    int i,count = alarm_list.index;
    for(i=0;i<count;i++){
        p_alarm = &alarm_list.alarms[i];
        p_alarm->report_value = *p_alarm->p_value;
    }
}

cJSON* get_alarm_info(){
    cJSON* param = cJSON_CreateObject();
    cJSON* device,*item;
    device_alarm* p_alarm;
    int i,count = alarm_list.index;
    for(i=0;i<count;i++){
        p_alarm = &alarm_list.alarms[i];
        device = cJSON_GetObjectItem(param,p_alarm->device);
        if(device == NULL) {
            device = cJSON_CreateArray();
            cJSON_AddItemToObject(param,p_alarm->device,device);
        }
        item = cJSON_CreateObject();
        cJSON_AddItemToObject(item,"status",cJSON_CreateNumber(*p_alarm->p_value));
        cJSON_AddItemToObject(item,"when1",cJSON_CreateString(p_alarm->value_string));
        cJSON_AddItemToArray(device,item);
        
        p_alarm->report_value = *p_alarm->p_value;    
    }
    return response_packet(id_alarm_check,NO_ERROR,param,0);
}

void scan_alarm(void* param){
    cJSON* infos = NULL;
    cJSON* device = NULL,*item = NULL;
    char* p_json;
    char send_buff[1024] = {0};
    user_socket* p_user = (user_socket*)param;
    
    device_alarm* p_alarm;
    int i,count = alarm_list.index;
    for(i=0;i<count;i++){
        p_alarm = &alarm_list.alarms[i];
        if(*p_alarm->p_value != p_alarm->report_value){            
            if(infos == NULL) infos = cJSON_CreateObject();
            device = cJSON_GetObjectItem(param,p_alarm->device);
            if(device == NULL) cJSON_AddItemToObject(param,p_alarm->device,cJSON_CreateArray());
            item = cJSON_CreateObject();
            cJSON_AddItemToObject(item,p_alarm->value_name,cJSON_CreateNumber(*p_alarm->p_value));
            cJSON_AddItemToObject(item,"when1",cJSON_CreateString(p_alarm->value_string));
            cJSON_AddItemToArray(device,item);    
        }        
    }
    if(infos){
        p_json = request_packet(id_alarm_report,infos);
        write_log("recv alarm report:\n%s",p_json);
        sprintf(send_buff,"%s%s",p_json,END_MARK);
        printf_comm_flag(COM_TX,print_alarm,p_user->username,send_buff,strlen(send_buff));
        send(p_user->sock,send_buff,strlen(send_buff),0);
        free(p_json);
    }
}

cJSON* alarm_parse(CMD_ID id,cJSON* param){
    cJSON* alarm_json = NULL;
    switch(id){
        case id_alarm_report:	
            reset_alarm_info();
        break;
        case id_alarm_check:
            alarm_json = get_alarm_info();
        break;        
    }
    return alarm_json;    
}